import { readdirSync } from 'fs';
import { readdir } from 'fs/promises';
import path from 'path';
export const getFileListSync = (dirName, og = dirName) => {
    const files = [];
    const items = readdirSync(dirName, { withFileTypes: true });
    for (const item of items) {
        const fullPath = path.join(dirName, item.name);
        if (item.isDirectory()) {
            files.push(...getFileListSync(fullPath, og));
        }
        else {
            files.push(path.relative(og, fullPath));
        }
    }
    return files;
};
// TODO consolidate this function
export const getFileListWithDirectories = (dirName, og = dirName) => {
    const files = [];
    const items = readdirSync(dirName, { withFileTypes: true });
    for (const item of items) {
        const fullPath = path.join(dirName, item.name);
        if (item.isDirectory()) {
            const relativeDir = path.relative(og, fullPath);
            if (relativeDir !== '.git') {
                files.push(relativeDir);
                files.push(...getFileListWithDirectories(fullPath, og));
            }
        }
        else {
            files.push(path.relative(og, fullPath));
        }
    }
    return files;
};
export async function* getFileList(dirName, og = dirName, ignored = new Set()) {
    const items = await readdir(dirName, { withFileTypes: true });
    ignored.add('.git');
    ignored.add('node_modules');
    for (const item of items) {
        if (ignored.has(item.name)) {
            continue;
        }
        if (item.isDirectory()) {
            yield* getFileList(`${dirName}/${item.name}`, og);
        }
        else {
            const path = `${dirName}/${item.name}`;
            yield path.startsWith(og) ? path.substring(og.length) : path;
        }
    }
}
